﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Library.Extensions;

namespace Script
{
    /// <summary>
    /// 生成excel插入用的格式
    /// </summary>
    public class CreateExcelCell : BaseClass
    {
        public CreateExcelCell()
        {
            CreateExcel();
        }

        protected virtual void CreateExcel()
        {
            var dir1 = SystemConsole.GetInputStr("请拖入选定文件:", "您选择的文件：", def: "new.txt");
            var dir2 = SystemConsole.GetInputStr("请拖入选定文件:", "您选择的文件：", def: "old.txt");

            var result = new HashSet<string>(File.ReadAllLines(dir1), StringComparer.OrdinalIgnoreCase);

            result.ExceptWith(File.ReadAllLines(dir2));

            foreach (var re in result.Select(p => dir1 + p).ForEachPaths())
            {
                var newName = re.Replace(dir1, dir1 + "res");
                new FileInfo(newName).Directory.Create();
                File.Copy(dir1 + re, newName, true);
            }

            var root = dir1 + "res";
            RunList(Directory.GetFiles(root, "*.*", SearchOption.AllDirectories), root);
        }

        protected void RunList(IEnumerable<string> res, string root)
        {
            Dictionary<string, string> dic = new Dictionary<string, string>();

            foreach (var re in res.Where(p => !string.IsNullOrEmpty(p)).ToList().ForEachPaths())
            {
                try
                {
                    foreach (var item in GetExcelCell(re).OfType<string>())
                    {
                        dic[re.Replace(root, "")] = item;
                    }
                    //var newName = @"D:\Work\mfxy\资料\小圆\xy\mfxy\res" + re.Replace(root, "");
                    //FileHelper.CreateDirectory(newName);
                    //File.Copy(re, newName);
                }
                catch (Exception e)
                {
                    Console.WriteLine(re + "\t" + e.Message);
                }
            }
            WriteAllLines(dic, root);
        }
    }
}